        TTL     > TOS01

; ***********************************
; ***    C h a n g e   L i s t    ***
; ***********************************

; Date       Name  Description
; ----       ----  -----------

; 10-Aug-88  SKS   0.63 Add debug for oscli and Edit startup
; 19-Oct-88  SKS   0.64 Add facility to exit when star command returns
;                       Add facility to do *go xxxx ; command tail

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

                GBLS    $version
$version        SETS    "0.64 (19 Oct 1988)"

                GBLL    $debugcli
$debugcli       SETL    1=0

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

                CPU     1       ; Allow CMOS opcodes

mybyt_A * 163                   ; OS_Byte values used to talk to emulator
mybyt_X * 243

sv_cli   * &03                  ; Illegal 6502 opcodes used to talk to emulator
sv_byte  * &13
sv_word  * &23
sv_wrch  * &33
sv_rdch  * &43
sv_file  * &53
sv_args  * &63
sv_bget  * &73
sv_bput  * &83
sv_gbpb  * &93
sv_find  * &A3
sv_exit  * &B3 ; NB. Don't execute this without looking at quit_star ...
sv_language * &C3

cNOP     * &EA

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

LF * 10
CR * 13

; Zero page workspace definitions

initptr * &EE
turflag * &F0 ; Don't give away !
pagevalue * &F1
cliptr * &F2 ; Must be &F2 for OSWORD cmd tail fuj
himemptr * &F4
number * &F6
tmpptr * &F8
vstrptr * &FA
tempa * &FC
brkptr * &FD
escflag * &FF

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; OS absolute workspace + addresses

                ^       &0200
vectors         #       &0036
 ASSERT veclength = &0036

lineb_length    *       &0300-@
linebuffer      #       lineb_length

turbo           *       &0301

origin          *       &F800
fixedcode       *       &FF00

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        ORG     origin

def_start ROUT ; Default address we will GO to

; .............................................................................
; Entry for *OS; *GO will return to caller

titlethenloop_star ROUT

        JSR     setOSbrk        ; Set up an error handler

        JSR     osnewl          ; Print OS title
        JSR     printtitle
        JSR     osnewl

        LDYIM   4               ; See if we've any code to execute
        JSR     HostByte
        TYA
        BNE     gotoXY          ; [some code to execute]

; .............................................................................

starloop
        JSR     vstring
;;;        =       23,17,5,0,0,0,0,0,0,0    ; looks silly in a task window
        =       "65*"
;;;        =       23,17,5,0,0,0,0,0,0,0
        NOP
        LDAIM   0
        LDXIM   :LSB: word0block
        LDYIM   :MSB: word0block
        JSR     osword
        BCS     escape
        LDXIM   :LSB: linebuffer
        LDYIM   :MSB: linebuffer
        JSR     oscli
        JMP     starloop

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

language_star                   ; Load bound language into core
        =       sv_language

; .............................................................................
; Come here if someone's running a 6502 program !

; Real GO address is stored by the emulator, who loaded our code

nmi_reallygo

        LDYIM   4               ; Read GO address from 'Host'
        JSR     HostByte
        TYA
        BEQ     def_start       ; [no code to execute]

; .............................................................................

gotoXY  JMP     startcodeXY

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

escape  LDAIM   &7E
        JSR     osbyte
        BRK
        =       17
        =       "Escape", 0

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

word0block
        &       linebuffer
        =       lineb_length-1
        =       " "
        =       255

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

reset ROUT ; Once off initialisation

        LDXIM   &FF
        TXS                     ; SP : = &FF
        JSR     reset_init

        LDYIM   6               ; See if we've a star command to execute
        JSR     HostByte
        TYA
        BNE     #FT90

        JSR     vstring
        =       LF
        =       "Acorn 6502 Tube", LF,CR
        NOP

        JMP     titlethenloop_star


90      JSR     setOSbrk        ; Set up an error handler

        JSR     oscli           ; Execute command

        JMP     quit_star       ; Terminate correctly if it returns

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; All we need is SP valid here !

reset_init ROUT

        CLD     ; Rather important

        LDXIM   veclen-1        ; Copy vectors into page 2
10      LDAAX   vecsstart
        STAAX   vectors
        DEX
        BPL     #BT10

        CLR     escflag         ; Clear ESCAPE
        CLR     turflag         ; Force normal operation

        LDXIM   :MSB: &0800
        STX     pagevalue       ; Default PAGE

        LDXIM   :LSB: (def_start) ; Default *GO address
        LDYIM   :MSB: (def_start)
        STX     himemptr
        STY     himemptr+1
        RTS

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

irqbrk  STA     tempa
        PLA
        PHA
        ANDIM   &10
        BNE     brk
        JMI     irq1v

; .............................................................................

irq1han JMI     irq2v

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Probably never executed due to emulator going JMI (brkv)

brk     TXA
        PHA
        TSX
        LDAAX   &103
        CLD
        SEC
        SBCIM   &01
        STA     brkptr
        LDAAX   &104
        SBCIM   &00
        STA     brkptr+1
        LDAIM   0
        STA     brkptr+turbo
        PLA
        TAX
        LDA     tempa
        CLI
        JMI     brkv

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

tim_brk ROUT

        LDXIM   &FF
        TXS
        INX
        STX     brkptr+turbo
        CLI
        JSR     osnewl

        LDYIM   1
10      LDAIY   brkptr
        BEQ     #FT50
        JSR     oswrch
        INY
        BNE     #BT10           ; Safer than BRA

50      JSR     osnewl
        JMP     starloop

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        LNK     TOS04
